<?php 
 require_once "../includes/pagination.php";
 $itemPerPage=15;

 
if(!empty($_REQUEST['delSubCategory']))
{ 	
  $categoryId=$_REQUEST['delSubCategory'];
  $sqlDelCat="Delete from tbl_category where category_id='$categoryId'";
  $rstDelCat=mysql_query($sqlDelCat);
  if($rstDelCat)	{			
    echo "<script type='text/javascript'>window.location='index.php?listscat';</script>";			
  }
}
 
$cond = '';
$sort = '';
$ut = $u = $fn = $ln = $dg = 'asc';
if(isset($_GET['sort']))
{
 if($_GET['sort']=='category_name')
 {
  if($_GET['s_type']=='asc')
  {	$cond=' ORDER BY category_name ASC';	$sort = '&sort=category_name&s_type=asc';	$ut='desc';	}
  else
  {	$cond=' ORDER BY category_name DESC';	$sort = '&sort=category_name&s_type=desc';	}
 }
}
 
if(!empty($_REQUEST['catId']))
{
  $catId=$_REQUEST['catId'];
  
  $sqlShwCat="SELECT * FROM tbl_category where category_id=$catId";
  $rstShwCat=mysql_query($sqlShwCat);
  $rowShwCat=mysql_fetch_array($rstShwCat);
  $categoryShwName=$rowShwCat['category_name'];
  
 $search='';
 if(isset($_POST['btnSearchPro'])) {
  $category_name 	= $_POST['category_name'];		
  if($category_name!='')
   $search.= " AND category_name='".$category_name."'";
 }
  
  
  
  $sqlShwSubcat="select * from tbl_category where  parent_id=$catId $search $cond";
  $rstShwSubcat=mysql_query(getPagingQuery($sqlShwSubcat,$itemPerPage));
  $numShwSubcat=mysql_num_rows($rstShwSubcat);
}
else { //thsi condition will execute only when search without selecting parent category
 $search='';
 if(isset($_POST['btnSearchPro'])) {
  $category_name 	= $_POST['category_name'];		
  if($category_name!='')
   $search.= " category_name='".$category_name."'";
   
  $sqlShwSubcat="select * from tbl_category where $search $cond";
  $rstShwSubcat=mysql_query(getPagingQuery($sqlShwSubcat,$itemPerPage));
  $numShwSubcat=mysql_num_rows($rstShwSubcat);
 }
 else
  $numShwSubcat=0;
}


 
 
?>
 <form action="" method="post" name="formDelCategory" id="formDelCategory">
 	<input type="hidden" name="delSubCategory" id="delSubCategory">
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Swapen Subcategory</td>
  </tr>  
  <tr>
    <td height="577">
	<table width="100%">
	    <tbody>
		<tr>
			<td>
				<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr>
						<td width="30%">Category Name</td>
						<td width="70%">
							<input type="text" name="category_name" id="category_name" />
						</td>
					</tr>
					

					<tr>
						<td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search Category" /></td>
					</tr>
				</table>
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<b>Sort  By :</b>
				<?
				if(!empty($_REQUEST['catId']))
				{
				 ?>
				 <a href="<?php echo $base_url;?>admin/index.php?listscat&catId=<?php echo $_REQUEST['catId']?>&sort=category_name&s_type=<?php echo $ut;?>">Category Name</a> &nbsp;&nbsp;&nbsp;
				 <?php
				}
				else
				{
				?>
				 <a href="<?php echo $base_url;?>admin/index.php?listscat&sort=category_name&s_type=<?php echo $ut;?>">Category Name</a> &nbsp;&nbsp;&nbsp;
				<?php
				}
				?>
				
			</td>
		</tr>
		</tbody>
	</table><br>
		<table width="100%"  border="0" cellspacing="0">
		 <tr><td colspan="2"><div align="center"><?php echo "<b>".$msgDelScat."</b>"; ?></div></td></tr>
		 <tr>
		  <td>
		   <div align="center">Category
		    <select name="cat_name" id="cat_name" onChange="javascript:categoryListSubmit(this.value)" class="fieldMax1">
		     <option value="">..Select Category..</option> 
		     <?php			   
		     $sqlSelCategory="SELECT * FROM tbl_category where parent_id=0";
		     $rstSelCategory=mysql_query($sqlSelCategory);
		     while($rowSelCategory=mysql_fetch_array($rstSelCategory))
		     {
		       $categoryId=$rowSelCategory['category_id'];
		       $categoryName=$rowSelCategory['category_name'];
		       
			if(!empty($_REQUEST['catId'])) {
			  if($categoryId==$_REQUEST['catId']) {
			    $select="selected";
			  } else {
			    $select="";
			  }
			}
			else { $select=""; }
			echo "<option value='$categoryId' $select>$categoryName</option>";
		     }
		     ?>
		    </select>
		   </div>
		  </td>
		 </tr>   			
		</table>
		
		<table width="100%"  border="0" cellspacing="0">
          <tr>
            <td colspan='6' style="text-align:right;"><input type="button" class="button" onclick="window.location='index.php?addsubcat'" value="Add Subcategory" /></td>
          </tr>
          <tr class="InnerHeader">
            <td width="2%"  class="line">&nbsp;</td>
            <td width="8%" class="line">Subcategory Name</td>
            <td width="8%" class="line">Description</td>
            <td width="2%" class="line"></td>
            <td width="2%" class="line"></td>
            <td width="2%" class="line"></td>
          </tr>
          <?php
			 //if(!empty($_REQUEST['catId'])) {
			 		 
			  if($numShwSubcat==0)
			  {
				 echo "<tr><td colspan='5' class='line'><div align='center'><h3>No Subcategories Found...</h3></div></td></tr>";
			  }
			  else
			  {
				  while($rowShwSubCat=mysql_fetch_array($rstShwSubcat))
				  {
					 $subcatId=$rowShwSubCat['category_id'];
					 $subcatName=$rowShwSubCat['category_name'];
					 $subcatDesc=$rowShwSubCat['category_description'];
					 $subcatImage=$rowShwSubCat['category_image']; 
					 echo "<tr>";
					 echo "<td class='line'>&nbsp;</td>";
					 echo "<td class='line'>$subcatName</td>";
					 echo "<td class='line'>$subcatDesc</td>";
					 echo "<td class='line'><input type='button' name='edit' value='View' class='button' onclick='editSubategory($subcatId);'></td>";
					 echo "<td class='line'><input type='button' name='edit' value='Edit' class='button' onclick='editSubategory($subcatId);'></td>";
					 echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='deleteSubCategory($subcatId);'></td> ";
					 echo "</tr>";
				  }
			  }
			//}
			 		
		 ?>
          <tr>
            <td colspan='6' style="text-align:right;"><input type="button" class="button" onclick="window.location='index.php?addsubcat'" value="Add Subcategory" />            </td>
          </tr>
        </table>
			<div align="center">
			<?php
				   $link="listscat&catId=$catId";
				    if(!empty($_REQUEST['catId']))
 					{
				   		echo $disp=getPagingLink($sqlShwSubcat,$itemPerPage,$link);
					}								   
			?>
		</div>
		</td>
  </tr>
</table>
</form>